﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boggle
{
    class Program
    {
        private static readonly object lock1 = new object();
        
        static void Main(string[] args)
        {
            BoggleConnector connector = new BoggleConnector("name");

            connector.ConnectToServer();

            connector.GameStarted += OnGameStarted;
            connector.GameStopped += OnGameStopped;
            connector.ScoreChanged += OnScoreChanged;
            connector.ConnectionClosed += OnConnectionClosed;
            connector.TimeChanged += OnTimeChanged;
            connector.Terminated += OnTerminated;
            connector.MessageSent += OnMessageSent;

            Console.ReadLine();
        }

        static void OnMessageSent(object sender, StringEventArgs e)
        {
            Console.WriteLine("Sent Message: {0}", e.text);
        }

        static void OnGameStarted(object sender, StringEventArgs e)
        {
            string[] start = e.text.Split(null);

            Console.WriteLine("Game Started\n\n" +
                              "   Board: " + start[0] + "\n" +
                              "    Time: " + start[1] + "\n" +
                              "Opponent: " + start[2]);

        }

        static void OnGameStopped(object sender, StringEventArgs e)
        {
            Console.WriteLine("Game Stopped: {0}", e.text);
        }

        static void OnScoreChanged(object sender, StringEventArgs e)
        {
            string[] scores = e.text.Split(null);

            Console.WriteLine("Player1 Score: {0}", scores[0]);
            Console.WriteLine("Player2 Score: {0}", scores[1]);
        }

        static void OnConnectionClosed(object sender, EventArgs e)
        {
            lock (lock1)
            {
                Console.WriteLine("Connection Closed");
                Console.Write("Press ENTER to exit.."); 
            }
        }

        static void OnTimeChanged(object sender, StringEventArgs e)
        {
            Console.WriteLine("Time Remaining: {0}", e.text);
        }

        static void OnTerminated(object sender, EventArgs e)
        {
            lock (lock1)
            {
                Console.WriteLine("Connection Terminated"); 
            }
        }

    }
}
